package com.dxq.oa.ontroller;

import com.dxq.oa.pub.VueData;
import com.dxq.oa.utils.VerifyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.Buffer;

@RestController
@Slf4j
public class CodeController {


    @GetMapping("/getCode")
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {

        log.info("获取验证码开始-------------------------");
        VueData code = VerifyCode.getCode();

        Image data = (Image) code.getData();

        String code1 = code.getCode();

        request.getSession().setAttribute("VerifyCode",code1);

        ServletOutputStream outputStream = response.getOutputStream();

        ImageIO.write( (BufferedImage)data, "jpg", outputStream);

        log.info("验证码响应结束-------------------------");
    }
}
